package com.github.caijh.mall.store.service.impl;

import java.util.List;
import javax.annotation.Resource;
import javax.inject.Inject;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.caijh.mall.common.PageParams;
import com.github.caijh.mall.common.enums.ProductTypeEnum;
import com.github.caijh.mall.store.dao.StoreProductDescriptionDao;
import com.github.caijh.mall.store.model.StoreProduct;
import com.github.caijh.mall.store.model.StoreProductDescription;
import com.github.caijh.mall.store.request.StoreProductDescriptionSearchRequest;
import com.github.caijh.mall.store.service.StoreProductDescriptionService;
import com.github.caijh.mall.system.service.SystemAttachmentService;
import com.github.pagehelper.PageHelper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * StoreProductDescriptionServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class StoreProductDescriptionServiceImpl extends ServiceImpl<StoreProductDescriptionDao, StoreProductDescription> implements StoreProductDescriptionService {

    @Resource
    private StoreProductDescriptionDao dao;

    @Inject
    private SystemAttachmentService systemAttachmentService;


    /**
     * 列表
     *
     * @param request    请求参数
     * @param pageParams 分页类参数
     * @return List<StoreProductDescription>
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @Override
    public List<StoreProductDescription> getList(StoreProductDescriptionSearchRequest request, PageParams pageParams) {
        PageHelper.startPage(pageParams.getPage(), pageParams.getLimit());

        //带 StoreProductDescription 类的多条件查询
        LambdaQueryWrapper<StoreProductDescription> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        StoreProductDescription model = new StoreProductDescription();
        BeanUtils.copyProperties(request, model);
        lambdaQueryWrapper.setEntity(model);
        return dao.selectList(lambdaQueryWrapper);
    }

    @Override
    public String findStoreProductDescription(int productId, int type) {
        StoreProductDescription productDescription = this.getOne(
                Wrappers.<StoreProductDescription>lambdaQuery()
                        .eq(StoreProductDescription::getProductId, productId)
                        .eq(StoreProductDescription::getType, type));
        if (productDescription == null || productDescription.getDescription() == null) {
            return "";
        }

        return productDescription.getDescription();
    }

    /**
     * 根据商品id和type删除对应描述
     *
     * @param productId 商品id
     * @param type      类型
     */
    @Override
    public void deleteByProductId(int productId, int type) {
        LambdaQueryWrapper<StoreProductDescription> lmq = Wrappers.lambdaQuery();
        lmq.eq(StoreProductDescription::getProductId, productId).eq(StoreProductDescription::getType, type);
        dao.delete(lmq);
    }

    /**
     * 删除旧的商品详情，保存新的商品详情.
     *
     * @param product 商品
     * @param content 商品内容
     */
    @Override
    public void updateStoreProductDescription(StoreProduct product, String content) {
        StoreProductDescription spd = new StoreProductDescription(
                product.getId(),
                Strings.isNotBlank(content) ? systemAttachmentService.clearPrefix(content) : "", ProductTypeEnum.NORMAL.getIndex());
        this.deleteByProductId(spd.getProductId(), ProductTypeEnum.NORMAL.getIndex());
        this.save(spd);
    }

}

